﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KP_TCP_Client
{
    public class MyConvert
    {
        /// <summary>
        /// 字符串转化byte数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static public byte[] StringToByteArray(string str)
        {
            return System.Text.Encoding.Default.GetBytes(str);
        }

        /// <summary>
        /// byte数据，转化为字符串
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        static public string ByteArrayToString(byte[] byteArray)
        {
            return System.Text.Encoding.Default.GetString(byteArray);
        }

        /// <summary>
        /// byte数据，转化为字符串
        /// </summary>
        /// <param name="byteArray"></param>
        /// <param name="len">需要转换的字节数</param>
        /// <returns></returns>
        static public string ByteArrayToString(byte[] byteArray, int len)
        {
            byte[] buf = new byte[len];
            for (int i = 0; i < len; i++)
            {
                buf[i] = byteArray[i];
            }
            return System.Text.Encoding.Default.GetString(buf);
        }

        /// <summary>
        /// 将字符串转为Ascii 的十六进制
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static public byte[] StringToAsciiByte(string str)
        {
            return System.Text.Encoding.ASCII.GetBytes(str);
        }

        /// <summary>
        /// 将Ascii的十六进制数组转化为字符串
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        static public string AsciiByteToString(byte[] byteArray)
        {
            return System.Text.Encoding.ASCII.GetString(byteArray);
        }

        /// <summary>
        /// 将Ascii的十六进制数组转化为字符串
        /// </summary>
        /// <param name="byteArray"></param>
        /// <param name="len">需要转换的字节数</param>
        /// <returns></returns>
        static public string AsciiByteToString(byte[] byteArray, int len)
        {
            byte[] buf = new byte[len];
            for (int i = 0; i < len; i++)
            {
                buf[i] = byteArray[i];
            }
            return System.Text.Encoding.ASCII.GetString(buf);
        }
    }
}
